<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-button type="primary">添加角色</el-button>
      <el-table :data="RolesList" border stripe>
        <el-table-column type="expand">
          <template v-slot="{ row }">
            <el-row
              v-for="(item, i) in row.children"
              :key="item.id"
              class="border_buttom"
              :class="i === 0 ? 'border_top ' : ''"
            >
              <el-col :span="5">
                <el-tag>{{ item.authName }}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col :span="19">
                <!-- 二次循环 -->
                <el-row
                  v-for="(item2, i2) in item.children"
                  :key="item2.id"
                  class="border_buttom"
                  :class="i2 !== 0 ? 'border_top' : ''"
                >
                  <el-col :span="6">
                    <el-tag type="success">{{ item2.authName }}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18">
                    <!-- 三次循环 -->
                    <el-row
                      v-for="(item3, i3) in item2.children"
                      :key="item3.id"
                      :class="i3 !== 0 ? 'border_top' : ''"
                    >
                      <el-col>
                        <el-tag
                          type="warning"
                          closable
                          @close="removeTag(row, item3.id)"
                          >{{ item3.authName }}</el-tag
                        >
                      </el-col>
                    </el-row>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="角色名称" prop="roleName"></el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              @click="editRoles(row)"
              >编辑
            </el-button>
            <el-button size="mini" type="danger" icon="el-icon-delete"
              >删除
            </el-button>
            <el-button
              @click="showset(row)"
              size="mini"
              type="warning"
              icon="el-icon-s-tools"
              >分配权限
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 分配权限对话框 -->
    <el-dialog
      @close="keys = []"
      title="分配权限"
      :visible.sync="setRightdialogVisible"
      width="50%"
    >
      <el-tree
        ref="tree"
        :data="rightsList"
        :props="treeProps"
        default-expand-all
        show-checkbox
        node-key="id"
        :default-checked-keys="keys"
      ></el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="setRightdialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="setRights">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 编辑按钮 -->
    <el-dialog title="编辑" :visible.sync="editDialogVisible" width="50%">
      <el-form ref="editFromRoles" label-width="80px" :model="editRolesValue">
        <el-form-item label="角色名称">
          <el-input v-model="editRolesValue.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="editRolesValue.roleDesc"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="rolesEditDialogVisible"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 编辑模块双向绑定
      editRolesValue: {
        roleName: '',
        roleDesc: '',
        id: ''
      },
      editDialogVisible: false,
      RolesList: [],
      setRightdialogVisible: false,
      rightsList: [],
      // 树形控件的配置对象
      treeProps: {
        // 将数据显示利用authName字段
        label: 'authName'
      },
      keys: [],
      rolesId: ''
    }
  },

  created() {
    this.getRoles()
  },

  methods: {
    // 数据请求
    async getRoles() {
      const res = await this.$axios.get('roles')
      // console.log(res)
      if (res.meta.status !== 200) return this.$message(res.meta.msg)
      this.RolesList = res.data
    },
    // 请求展开栏 删除
    async removeTag(row, itemId) {
      // console.log('删除权限', row.id, itemId)
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const res = await this.$axios.delete(
            `roles/${row.id}/rights/${itemId}`
          )
          if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
          console.log(res)
          row.children = res.data
          this.$message.success(res.meta.msg)
        })
        .catch(() => {
          this.$message('已取消删除')
        })
    },

    // 分配权限
    async showset(row) {
      // console.log(row)
      this.rolesId = row.id
      this.geiAllRightsId(row)
      this.setRightdialogVisible = true
      const res = await this.$axios.get('rights/tree')

      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.rightsList = res.data
    },
    geiAllRightsId(data) {
      if (!data.children) {
        this.keys.push(data.id)
        return
      }
      data.children.forEach(item => {
        this.geiAllRightsId(item)
      })
    },
    async setRights() {
      const allkey = [
        ...this.$refs.tree.getCheckedKeys(),
        ...this.$refs.tree.getHalfCheckedKeys()
      ]
      // const a = allkey.join(',')
      // console.log(a)

      const res = await this.$axios.post(`roles/${this.rolesId}/rights`, {
        rids: allkey.join(',')
      })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.$message.success(res.meta.msg)
      this.getRoles()
      this.setRightdialogVisible = false
      console.log(res)
    },
    // 编辑按钮
    editRoles({ roleName, roleDesc, id }) {
      this.editDialogVisible = true
      this.editRolesValue.roleName = roleName
      this.editRolesValue.roleDesc = roleDesc
      this.editRolesValue.id = id
    },
    // 编辑按钮里触发axios事件重新更新数据
    async rolesEditDialogVisible() {
      const res = await this.$axios.put('roles/' + this.editRolesValue.id, {
        roleName: this.editRolesValue.roleName,
        roleDesc: this.editRolesValue.roleDesc
      })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.$message.success(res.meta.msg)
      this.getRoles()
      this.editDialogVisible = false
      // console.log(res)
    }
  }
}
</script>

<style scoped lang='less'>
.el-tag {
  margin: 8px;
}
.border_top {
  border-top: 1px solid #eee;
}
.border_buttom {
  border-bottom: 1px solid #eee;
}
.el-row {
  display: flex;
  align-items: center;
}
</style>
